package com.graduce.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.graduce.commonutils.R;
import com.graduce.servicebase.exceptionhandler.graduceException;
import com.graduce.vod.Utils.ConstantVodUtils;
import com.graduce.vod.Utils.InitVodClient;
import com.graduce.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Api(description="上传视频")

@RestController
@RequestMapping("/eduvod/video")

public class VodController {

    @Autowired

    private VodService vodService;

    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file){
       String videoId = vodService.uploadVideoAly(file);
       return R.ok().data("videoId",videoId);
   }

//    //根据视频id删除阿里云视频
//    @ApiOperation(value = "根据视频id删除阿里云视频")
//    @DeleteMapping("removeAlyVideo/{id}")
//    public R removeAlyVideo(@PathVariable String id) {
//        vodService.removeAlyVideo(id);
//        return R.ok();
//    }
//
    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @ApiOperation(value = "删除多个阿里云视频")
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }

    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch(Exception e) {
            throw new graduceException(20001,"获取凭证失败");
        }
    }

}
